//
//  AddNewBuyersViewModel.m
//  GLGW_Shoping
//
//  Created by yang on 2018/12/10.
//  Copyright © 2018年 yang. All rights reserved.
//

#import "AddNewBuyersViewModel.h"
#import "NoDataFootTableViewHeaderFooterView.h"
#import "AdressInputTableViewCell.h"
#import "AdressInforTextViewTableViewCell.h"
#import "ServiceAddPictureTableViewCell.h"
#import "AdressFootView.h"
#import "AdressMarkModel.h"

@interface AddNewBuyersViewModel ()<UITableViewDelegate,UITableViewDataSource>{
    NSArray * leftPlaceholderArr;
    AdressFootView * footView;
    //
    ServiceBuyersModel_Infos * selfInforModel;
}
@end

@implementation AddNewBuyersViewModel

-(instancetype)initWithViewToViewModel:(UITableView *)tableView theSuperView:(UIView *)superView theInforsModel:(nonnull ServiceBuyersModel_Infos *)serInforModel{
    if (self = [super initWithViewToViewModel:tableView theSuperView:superView]) {
        if (serInforModel) {
            selfInforModel = serInforModel;
        }else{
            selfInforModel = [ServiceBuyersModel_Infos new];
        }
        leftPlaceholderArr = @[@"请输入接货人姓名",@"请输入接货人电话",@"接货地址"];
        tableView.mj_header.hidden = YES;
        tableView.mj_footer.hidden = YES;
        tableView.dataSource= self;
        tableView.delegate = self;
        [tableView registerClass:[NoDataFootTableViewHeaderFooterView class] forHeaderFooterViewReuseIdentifier:@"NoDataFootTableViewHeaderFooterView"];
        tableView.tableFooterView = [self createFootView];
        [superView addSubview:tableView];
        [self inputTextFieleChange];
    }
    return self;
}

-(UIView *)createFootView{
    footView = [[AdressFootView alloc] initWithFrame:CGRectMake(0, 0, KScreenWidth, kHeightProportion_In_iPhoneTriploid(110))];
    [[footView.severBut rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(__kindof UIControl * _Nullable x) {
        [self requestServiceData];
    }];
    return footView;
}
#pragma mark -- 数据请求
-(void)requestServiceData{
    NSString * apiStr;
    if (selfInforModel.server_id>0) {
        apiStr = kModifyServiceBuyer;
    }else{
        apiStr = kAppendserviceBuyer;
    }
    NSDictionary * dic = @{@"id":selfInforModel.server_id>0?[NSString stringWithFormat:@"%ld",(long)selfInforModel.server_id]:@"",
                           @"enterpriseName":koString(selfInforModel.enterpriseName),
                           @"contactPerson":koString(selfInforModel.contactPerson),
                           @"contactPhone":koString(selfInforModel.contactPhone),
                           @"warehouseName":koString(selfInforModel.warehouseName),
                           @"receiverName":koString(selfInforModel.receiverName),
                           @"receiverPhone":koString(selfInforModel.receiverPhone),
                           @"province":koString(selfInforModel.province),
                           @"city":koString(selfInforModel.city),
                           @"county":koString(selfInforModel.county),
                           @"address":koString(selfInforModel.address),
                           @"freeManagerId":koString(self.userId),
                           @"status":selfInforModel.server_id>0?@0:@1};
    [GLGWRequestTool POST:[NSString stringWithFormat:@"%@%@",kApiPrefix,apiStr] lodingView:nil lodingTextString:@"" isLoding:YES parameters:dic success:^(id  _Nullable response) {
        BaseModel * bModel = [BaseModel yy_modelWithJSON:response];
        if (bModel.code == 0) {
            if (self.delegate && [self.delegate respondsToSelector:@selector(requestDataSuccess:)]) {
                if (self->selfInforModel.server_id>0) {
                    [self.delegate requestDataSuccess:AddOrEditType_Edit];
                }else{
                    [self.delegate requestDataSuccess:AddOrEditType_Add];
                }
            }
        }
    } failure:^(NSError * _Nullable error) {
        
    }];
}

-(void)cancelTheCooperation:(NSInteger)statusType{
    NSDictionary * dic = @{@"id":[NSString stringWithFormat:@"%ld",(long)selfInforModel.server_id],@"status":@(statusType)};
    [GLGWRequestTool POST:[NSString stringWithFormat:@"%@%@",kApiPrefix,kModifyServiceBuyerStatus] lodingView:nil lodingTextString:@"" isLoding:YES parameters:dic success:^(id  _Nullable response) {
        BaseModel * bModel = [BaseModel yy_modelWithJSON:response];
        if (bModel.code == 0) {
            if (self.delegate && [self.delegate respondsToSelector:@selector(requestDataSuccess:)]) {
                [self.delegate requestDataSuccess:AddOrEditType_Edit];
            }
        }
    } failure:^(NSError * _Nullable error) {
        
    }];
}
#pragma mark -- UITableViewDelegate,UITableViewDataSource
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 2;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    if (section == 0) {
        return 2;
    }else{
        return 4;
    }
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    CGFloat cellHeight = kHeightProportion_In_iPhoneTriploid(44);
    if (indexPath.section == 0 && indexPath.row == 1) {
        cellHeight = kHeightProportion_In_iPhoneTriploid(69);
    }
    if (indexPath.section == 1 && indexPath.row == 3) {
        cellHeight = kHeightProportion_In_iPhoneTriploid(60);
    }
    return cellHeight;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    BaseTableViewCell * cell;
    switch (indexPath.section) {
        case 0:{
            if (indexPath.row == 0) {
                AdressInputTableViewCell * cell_input = [AdressInputTableViewCell cellWithTableView:tableView];
                cell_input.inputTextField.placeholder = @"请填写企业名称";
                cell_input.rowNumPath = indexPath;
                cell_input.service_Model = selfInforModel;
                cell_input.Block_AdressChange = ^(NSString * _Nonnull inputStr) {
                    [self inputTextFieleChange];
                };
                cell_input.inputTextField.clearButtonMode=UITextFieldViewModeWhileEditing;
                cell = cell_input;
            }else{
                ServiceAddPictureTableViewCell * cell_picture = [ServiceAddPictureTableViewCell cellWithTableView:tableView];
                cell = cell_picture;
            }
            
        }
            break;
        case 1:{
            if (indexPath.row == 3) {
                AdressInforTextViewTableViewCell * cell_textView = [AdressInforTextViewTableViewCell cellWithTableView:tableView];
                cell_textView.input_str = selfInforModel.address;
                kWeakSelf(self);
                cell_textView.Block_TextViewChange = ^(NSString * _Nonnull inputText) {
                    self->selfInforModel.address = inputText;
                    [weakself inputTextFieleChange];
                };
                cell = cell_textView;
            }else{
                AdressInputTableViewCell * cell_input = [AdressInputTableViewCell cellWithTableView:tableView];
                cell_input.inputTextField.placeholder = leftPlaceholderArr[indexPath.row];
                cell_input.rowNumPath = indexPath;
                cell_input.service_Model = selfInforModel;
                if (indexPath.row == 2) {
                    cell_input.showPush = YES;
                    cell_input.inputTextField.userInteractionEnabled = NO;
                }else{
                    cell_input.inputTextField.clearButtonMode=UITextFieldViewModeWhileEditing;
                }
                cell_input.Block_AdressChange = ^(NSString * _Nonnull inputStr) {
                    [self inputTextFieleChange];
                };

                cell = cell_input;
            }
        }
            break;
            
        default:
            break;
    }
    [cell setSepartionLineWith:lineModel_Half];
    return cell;
}

-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    NoDataFootTableViewHeaderFooterView * headerView = [tableView dequeueReusableHeaderFooterViewWithIdentifier:@"NoDataFootTableViewHeaderFooterView"];
    return headerView;
}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return kHeightProportion_In_iPhoneTriploid(10);
}

-(UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    NoDataFootTableViewHeaderFooterView * footView = [tableView dequeueReusableHeaderFooterViewWithIdentifier:@"NoDataFootTableViewHeaderFooterView"];
    return footView;
}

-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 0.01;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.section == 1 && indexPath.row == 2) {
        if (self.delegate && [self.delegate respondsToSelector:@selector(selectAdress)]) {
            [self.delegate selectAdress];
        }
    }
}

-(void)inputTextFieleChange{
    if (selfInforModel.enterpriseName.length>0 && selfInforModel.receiverName.length>0 && selfInforModel.receiverPhone.length>0 && selfInforModel.address.length>0) {
        footView.severBut.backgroundColor = kColor_Rule_Yellow;
        footView.severBut.enabled = YES;
    }else{
        footView.severBut.backgroundColor = [UIColor colorWithHexString:@"#CCCCCC"];
        footView.severBut.enabled = NO;
    }
}
@end
